﻿using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml.Linq;

namespace XML翻译工具
{
    internal class Classtool
    {
        /// <summary>
        /// 开始翻译软件
        /// </summary>
        /// <param name="Original_language"></param>
        /// <param name="target_language"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static string Main_tool(string Original_language, string target_language, string body)
        {
            // 改成您的APP ID
            string appId = "20221008001377862";
            Random rd = new Random();
            string salt = rd.Next(100000).ToString();
            // 改成您的密钥
            string secretKey = "667P1q9TE7j_5_beBpP4";
            string sign = EncryptString(appId + body + salt + secretKey);
            string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
            url += "q=" + HttpUtility.UrlEncode(body);
            url += "&from=" + Original_language;
            url += "&to=" + target_language;
            url += "&appid=" + appId;
            url += "&salt=" + salt;
            url += "&sign=" + sign;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            JObject jsonObject = JObject.Parse(retString);
            JArray transResults = (JArray)jsonObject["trans_result"];
            JObject firstResult = (JObject)transResults[0];
            string dstValue = (string)firstResult["dst"];
            Console.WriteLine(dstValue);
            return dstValue;
        }
        // 计算MD5值
        public static string EncryptString(string str)
        {
            MD5 md5 = MD5.Create();
            // 将字符串转换成字节数组
            byte[] byteOld = Encoding.UTF8.GetBytes(str);
            // 调用加密方法
            byte[] byteNew = md5.ComputeHash(byteOld);
            // 将加密结果转换为字符串
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteNew)
            {
                // 将字节转换成16进制表示的字符串，
                sb.Append(b.ToString("x2"));
            }
            // 返回加密的字符串
            return sb.ToString();
        }
        /// <summary>
        /// 读取xml文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static XDocument xDocument(string path)
        {
            XDocument document = XDocument.Load(path);
            return document;
        }
    }
}
